package com.hulizuofang.wol.chat.client.ui.chat;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import com.hulizuofang.wol.chat.client.bundle.ClientResourceBundle;
import com.hulizuofang.wol.chat.client.data.ChatUser;
import com.hulizuofang.wol.chat.client.ui.event.sendmessage.SendMessageEvent;

public class ChatPanel extends Composite {
	private SimplePanel container;
	private VerticalPanel verticalPanel;
	private TextBox textBox;
	private Button button;
	private final EventBus eventBus;
	private final String login;
	private final ChatUser callee;
	private final ClientResourceBundle bundle;

	@AssistedInject
	public ChatPanel(EventBus eventBus, ClientResourceBundle bundle, @Assisted String login, @Assisted ChatUser callee) {
		this.eventBus = eventBus;
		this.bundle = bundle;
		this.login = login;
		this.callee = callee;
		container = new DecoratorPanel();
		AbsolutePanel absolutePanel = new AbsolutePanel();
		verticalPanel = new VerticalPanel();
		absolutePanel.add(verticalPanel);
		FlowPanel flowPanel = new FlowPanel();
		textBox = new TextBox();
		flowPanel.add(textBox);
		button = new Button();
		flowPanel.add(button);
		absolutePanel.add(flowPanel);
		container.add(absolutePanel);
		initWidget(container);
	}
	
	@Override
	protected void onLoad() {
		textBox.addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (! event.isShiftKeyDown() && event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
					submit();
				}
			}
		});
		button.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				submit();
			}
		});
	}

	protected void submit() {
		String message = textBox.getValue();
		SendMessageEvent.fire(eventBus, callee, message);
	}
	
	private void message(IsWidget widget) {
		verticalPanel.add(widget);
	}

	private void message(String sender, String message) {
		FlowPanel flow = new FlowPanel();
		flow.add(new Label(sender));
		flow.add(new Label(message));
		message(flow);
	}
	
	public void addMyMessage(String message) {
		message(login, message);
	}

	public void addOtherMessage(String message) {
		message(callee.getLogin(), message);
	}
	
	public void addErrorMessage(String message, String error) {
		SafeHtmlBuilder builder = new SafeHtmlBuilder();		
		builder
		.appendEscaped("Error: ")
		.appendHtmlConstant("<i>")
		.appendEscaped(error)
		.appendHtmlConstant("</i>")
		.appendEscaped(" While delivering: ")
		.appendHtmlConstant("<b>")
		.appendEscaped(message)
		.appendHtmlConstant("</b>");
		
		HTML html = new HTML(builder.toSafeHtml());
		html.addStyleName(bundle.css().errorMessage());
		message(html);
	}

	public void addUndeliveredMessage(String message) {
		SafeHtmlBuilder builder = new SafeHtmlBuilder();		
		builder
		.appendEscaped("Could not deliver: ")
		.appendHtmlConstant("<b>")
		.appendEscaped(message)
		.appendHtmlConstant("</b>");
		
		HTML html = new HTML(builder.toSafeHtml());
		html.addStyleName(bundle.css().errorMessage());
		message(html);
	}
}
